package com.itheima.controller;


import com.itheima.bean.pojo.User;
import com.itheima.bean.vo.*;
import com.itheima.bean.exception.BusinessException;
import com.itheima.service.UserService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user") //请求路径（一级路径）
@CrossOrigin//解决跨域问题
public class UserController {

    @Autowired  //从springboot的IOC容器中，获取类型为UserService的对象
    private UserService userService;


    /**
     * 分页查询用户数据
     *
     * @param currentPage 页码
     * @param pageSize    每页条数
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}/{search}")
    @CrossOrigin //允许跨域
    public Result selectUserByPage(@PathVariable Integer currentPage,
                                   @PathVariable Integer pageSize,
                                   @PathVariable String search ){
        //假设出现了业务异常：
        if(pageSize <= 0){
            //抛出异常
            throw new BusinessException(Code.BUSINESS_ERR,"每页条数不能小于0");
        }

        //创建分页对象
        Pagination pagination = new Pagination();
        pagination.setCurrentPage(currentPage);
        pagination.setPageSize(pageSize);

        //调用业务功能:   用户分页查询
        PageBean pageBean = userService.queryUserByPage(pagination);

        //创建响应结果对象(把要响应的结果封装到该对象中)
        Result responseResult = new Result(Code.GET_ERR, "查询失败");
        //业务处理判断:业务层执行结果是否成功
        if (pageBean != null) {
            //把查询的结果封装到响应对象中
            responseResult = new Result(Code.GET_OK, pageBean, "查询成功");
        }

        return responseResult;
    }

    /**
     * 添加用户
     *
     * @param user 前端传递的请求参数对象：要添加的用户
     * @return
     */
    @PostMapping
    @CrossOrigin
    public Result addUser(@RequestBody AddUser user) {
        System.out.println("添加用户:" + user);
        //调用业务层：添加用户
        boolean result = userService.addUserAndRole(user);

        //创建响应结果对象（把要响应的结果封装到该对象中）
        Result responseResult = new Result(Code.SAVE_ERR, "添加失败");
        if (result) {
            responseResult = new Result(Code.SAVE_OK, "添加成功");
        }

        //响应结果对象
        return responseResult;

    }


    /**
     * 删除用户
     *
     * @param user
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteUser(@RequestBody DeleteUser user) {
        System.out.println("删除用户:"+user);
        //调用业务层：删除用户
        boolean result = userService.deleteUserAndRole(user);

        //创建响应结果对象（把要响应的结果封装到该对象中）
        Result responseResult = new Result(Code.DELETE_ERR, "删除失败");
        if (result){
            responseResult = new Result(Code.DELETE_OK, "删除成功");
        }
        return responseResult;
    }

    /**
     * 更改用户
     *
     * @param user
     * @return
     */
    @PutMapping
    @CrossOrigin
    public Result updateUser(@RequestBody UpdateUser user){
        System.out.println("更改用户:"+user);
        //调用业务层：更改用户
        boolean result = userService.updateUserAndRole(user);

        //创建响应结果对象（把要响应的结果封装到该对象中）
        Result responseResult = new Result(Code.UPDATE_ERR, "更改失败");
        if (result){
            responseResult = new Result(Code.UPDATE_OK, "更改成功");
        }
        return responseResult;
    }

    @GetMapping("/{username}")
    public Result getUserByName(@PathVariable String username){
        PageBean<User> userPageBean = userService.getUserByName(username);
        return new Result(Code.GET_OK,"用户查询成功!",userPageBean);
    }
}
